package edu.fhsu.csci468;

import javax.swing.JFrame;
import javax.swing.JDesktopPane;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;

import javax.swing.JButton;

public class SensorGUI extends JFrame {

	private static final long serialVersionUID = 1L;
	private JDesktopPane jDesktopPane = null;
	private JButton jButtonTrigger = null;
	private JButton jButtonReset = null;
	private JButton jButtonDisconect = null;
	private Socket sock;
	private ObjectOutputStream outStream;
	private Object comboBoxSelection;

	
	/**
	 * This is the default constructor
	 */
	public SensorGUI(String ip, Object comboBoxSelection) {
		super();
		this.comboBoxSelection = comboBoxSelection;
        this.setLocationRelativeTo(null);
		initialize(comboBoxSelection.toString());
		getConnection(ip);
	}
	
    public void closeWindow()
    {
    	this.dispose();
    }
	
	public void getConnection(String ip)
	{
		try
		{
			sock = new Socket(InetAddress.getByName(ip), Ports.SENSOR_PORT);
			System.out.println("Contected to Sever " + ip);
				
			outStream = new ObjectOutputStream(
					new BufferedOutputStream(sock.getOutputStream()));
			outStream.flush();
				
			outStream.writeObject(((SensorKind)comboBoxSelection).getSensorType());
			outStream.flush();
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
	}
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize(String title) {
		this.setSize(208, 179);
		this.setContentPane(getJDesktopPane());
		this.setTitle(title);
	}

	/**
	 * This method initializes jDesktopPane	
	 * 	
	 * @return javax.swing.JDesktopPane	
	 */
	private JDesktopPane getJDesktopPane() {
		if (jDesktopPane == null) {
			jDesktopPane = new JDesktopPane();
			jDesktopPane.add(getJButtonTrigger(), null);
			jDesktopPane.add(getJButtonReset(), null);
			jDesktopPane.add(getJButtonDisconect(), null);
		}
		return jDesktopPane;
	}

	/**
	 * This method initializes jButtonTrigger	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonTrigger() {
		if (jButtonTrigger == null) {
			jButtonTrigger = new JButton();
			jButtonTrigger.addActionListener(new Trigger());
			jButtonTrigger.setBounds(new Rectangle(30, 15, 136, 31));
			jButtonTrigger.setText("Trigger Sensor");
		}
		return jButtonTrigger;
	}

	/**
	 * This method initializes jButtonReset	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonReset() {
		if (jButtonReset == null) {
			jButtonReset = new JButton();
			jButtonReset.addActionListener(new Reset());
			jButtonReset.setEnabled(false);
			jButtonReset.setBounds(new Rectangle(30, 60, 137, 31));
			jButtonReset.setText("Reset Sensor");
		}
		return jButtonReset;
	}

	/**
	 * This method initializes jButtonDisconect	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonDisconect() {
		if (jButtonDisconect == null) {
			jButtonDisconect = new JButton();
			jButtonDisconect.addActionListener(new Disconnect());
			jButtonDisconect.setBounds(new Rectangle(30, 105, 134, 31));
			jButtonDisconect.setText("Disconnect");
		}
		return jButtonDisconect;
	}

    private class Trigger implements ActionListener
    {
    	public void actionPerformed(ActionEvent event)
    	{
    		jButtonReset.setEnabled(true);
    		jButtonTrigger.setEnabled(false);
    		try
    		{
        		outStream.writeObject(SensorAction.Trigger);
        		outStream.flush();
    		}
    		catch(IOException e)
    		{
    			e.printStackTrace();
    		}
    	}
    }
	
    private class Reset implements ActionListener
    {
    	public void actionPerformed(ActionEvent event)
    	{
    		jButtonReset.setEnabled(false);
    		jButtonTrigger.setEnabled(true);
    		try
    		{
        		outStream.writeObject(SensorAction.Reset);
        		outStream.flush();
    		}
    		catch(IOException e)
    		{
    			e.printStackTrace();
    		}
    	}
    }
	
	private class Disconnect implements ActionListener
    {
    	public void actionPerformed(ActionEvent event)
    	{
    		try
    		{
        		outStream.writeObject(SensorAction.Discontect);
        		outStream.flush();
    			sock.close();	
    		}
    		catch(IOException e)
    		{
    			e.printStackTrace();
    		}
    		closeWindow();
    	}
    }

}  //  @jve:decl-index=0:visual-constraint="203,75"
